package cn.edu.zafu.bigdata.zengyadian;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class TCPClient {
    public static void main(String[] args) {
        try {
            // 创建套接字并连接到服务器
            Socket socket = new Socket("xxx的服务器IP地址", 8888);
            System.out.println("已连接到服务器。");

            // 获取输入输出流
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

            BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));

            while (true) {
                // 获取用户输入的消息
                System.out.print("请输入要发送给服务器的消息（输入'quit'退出）：");
                String clientMessage = userInput.readLine();
                if (clientMessage.equals("quit")) {
                    break;
                }

                // 发送消息给服务器
                out.println(clientMessage);

                // 读取服务器回复的消息
                String serverReply = in.readLine();
                System.out.println("服务器回复： " + serverReply);
            }

            // 关闭连接
            socket.close();
            System.out.println("已与服务器断开连接。");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

